﻿using Microsoft.UI.Xaml.Data;

namespace DimensionsHelper.Client.WinUi.Helpers;

public partial class MessageTypeToBrushConverter : IValueConverter
{
    public Brush InfoBrush { get; set; } = (SolidColorBrush)Application.Current.Resources["TextFillColorPrimaryBrush"];

    public Brush WarnBrush { get; set; } = new SolidColorBrush(Colors.Orange);

    public Brush ErrorBrush { get; set; } = new SolidColorBrush(Colors.Red);

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value is MessageType type)
        {
            return type switch
            {
                MessageType.Warning => WarnBrush,
                MessageType.Error => ErrorBrush,
                _ => InfoBrush
            };
        }

        return InfoBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        if (value is SolidColorBrush solidBrush)
        {
            if (solidBrush.Color == ((SolidColorBrush)WarnBrush).Color)
            {
                return MessageType.Warning;
            }

            if (solidBrush.Color == ((SolidColorBrush)ErrorBrush).Color)
            {
                return MessageType.Error;
            }
        }

        return MessageType.Information;
    }
}